package special;

import org.junit.Test;

/**
 * @author kunkun
 * @className LeetCode_169
 * @Description ...
 * @date 2025/3/22 18:52
 */
public class LeetCode_169 {

    @Test
    public void test(){
        System.out.println(majorityElement(new int[]{3,2,3}));
    }

    //摩尔投票
    public int majorityElement(int[] nums) {
        int cur=nums[0],count = 1;
        for(int i=1;i<nums.length;i++){
            if(cur==nums[i]){
                count++;
            }else{
                count--;
            }
            if(count<=0){
                cur = nums[i];
                count *=-1;
            }
        }
        return cur;
    }


}
